﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Text;
[assembly: WebResource("HZ.Supervise.Web.Common.Resource.PCASClass.js", "text/javascript")]
namespace HZ.Supervise.Web.Common
{
    /// <summary>
    /// 地区选择控件
    /// 设置固定默认区域  InitAreaID
    /// 设置可选默认区域  DefaultAreaID 
    /// 获取选择后的区域 SelectedAreaID
    /// 吗默认选择的区域 DefaultAreaIDs
    /// </summary>
    public partial class AreaControlsDL : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化组件状态
            Initialize();
        }
        /// <summary>
        /// 呈现前预处理
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!Page.ClientScript.IsClientScriptBlockRegistered("AreaControls_PCASDLClass"))
            {
                //注册脚本库
                string jsResource = Page.ClientScript.GetWebResourceUrl(typeof(AreaControls), "HZ.Supervise.Web.Common.Resource.PCASDLClass.js");
                Page.ClientScript.RegisterClientScriptInclude(typeof(string), "AreaControls_PCASDLClass", jsResource);

            }

            StringBuilder sb = new StringBuilder("<script language=\"Javascript\">\n");
            sb.Append("new PCAS(\"" + this.ddlProvicedl.UniqueID + "\", \"" + this.ddlCitydl.UniqueID + "\"");
            sb.Append(", \"" + this.ddlCountrydl.UniqueID + "\"");


            sb.Append(DefaultAreaIDs);
            sb.Append(");");
            sb.Append("\n</script>\n");

            if (!Page.ClientScript.IsStartupScriptRegistered("areaControls_key2"))
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "areaControls_key2", sb.ToString());
            }
            base.OnPreRender(e);
        }

        /// <summary>
        /// 是否显示区县
        /// </summary>
        public bool IsShowCountry
        {
            set
            {
                ViewState["IsShowCountry"] = value;
            }
            get
            {
                object obj = ViewState["IsShowCountry"];
                return (null == obj) ? true : (bool)obj;
            }
        }

        /// <summary>
        /// 初始地区ID设置固定默认区域
        /// </summary>
        public string InitAreaID
        {
            set
            {
                ViewState["InitAreaID"] = value;
            }
            get
            {
                object obj = ViewState["InitAreaID"];
                return (null == obj) ? "" : (string)obj;
            }
        }

        /// <summary>
        /// 初始地区ID设置可选默认区域
        /// </summary>
        public string DefaultAreaID
        {
            set
            {
                ViewState["DefaultAreaID"] = value;
            }
            get
            {
                object obj = ViewState["DefaultAreaID"];
                return (null == obj) ? "" : (string)obj;
            }
        }

        /// <summary>
        /// 设置省级下拉框是否可选
        /// </summary>
        public string ddlProviceEnabled { get; set; }

        /// <summary>
        /// 设置市级下拉框是否可选
        /// </summary>
        public string ddlCityEnabled { get; set; }

        /// <summary>
        /// 设置县区下拉框是否可选
        /// </summary>
        public string ddlCountryEnabled { get; set; }

        /// <summary>
        /// 初始化控件状态
        /// </summary>
        private void Initialize()
        {
            //处理固定InitAreaID
            if (!string.IsNullOrEmpty(InitAreaID))
            {
                if (RegexValid(InitAreaID, "^[0-9]{6}$"))
                {
                    if (InitAreaID == "510000")
                    {
                        //如果指定了省份，则固定其选择项
                        this.ddlProvicedl.Attributes.Add("disabled", "disabled");
                    }
                    else if (InitAreaID.Substring(4, 2) == "00")
                    {
                        //如果指定了城市，则固定其选择项
                        //this.ddlCountry.Attributes.Add("disabled", "disabled");
                        this.ddlCitydl.Attributes.Add("disabled", "disabled");
                        this.ddlProvicedl.Attributes.Add("disabled", "disabled");
                    }

                    //县区固定其选项 20110516 ml
                    else
                    {
                        this.ddlCitydl.Attributes.Add("disabled", "disabled");
                        this.ddlProvicedl.Attributes.Add("disabled", "disabled");
                        this.ddlCountrydl.Attributes.Add("disabled", "disabled");
                    }
                }
            }
            //处理可选DefaultAreaID
            //if (!string.IsNullOrEmpty(DefaultAreaID))
            //{
            //    this.ddlProvice.Attributes.Add("disabled", "disabled");
            //}
            //省级下拉框是否可选
            if (!string.IsNullOrEmpty(ddlProviceEnabled) && ddlProviceEnabled.ToLower() == "false")
            {
                this.ddlProvicedl.Attributes.Add("disabled", "disabled");
            }
            //市级下拉框是否可选
            if (!string.IsNullOrEmpty(ddlCityEnabled) && ddlCityEnabled.ToLower() == "false")
            {
                this.ddlCitydl.Attributes.Add("disabled", "disabled");
            }
            //县区下拉框是否可选
            if (!string.IsNullOrEmpty(ddlCountryEnabled) && ddlCountryEnabled.ToLower() == "false")
            {
                this.ddlCountrydl.Attributes.Add("disabled", "disabled");
            }
            //是否显示区县选择框
            if (!IsShowCountry)
            {
                this.ddlCountrydl.Visible = false;
            }

        }

        /// <summary>
        /// 选择后的地区ID
        /// </summary>
        public string SelectedAreaID
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                string areaId = "";

                //首次进入使用当前用户所在区域ID
                areaId = string.IsNullOrEmpty(DefaultAreaID) ? InitAreaID : DefaultAreaID;

                //优先获取区县，其次城市，再次省份
                if (!string.IsNullOrEmpty(Request.Form[this.ddlCountrydl.Name]))
                {
                    areaId = Request.Form[this.ddlCountrydl.Name];
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request.Form[this.ddlCitydl.Name]))
                    {
                        areaId = Request.Form[this.ddlCitydl.Name];
                    }
                    else if (!string.IsNullOrEmpty(Request.Form[this.ddlProvicedl.Name]))
                    {
                        areaId = Request.Form[this.ddlProvicedl.Name];
                    }

                }

                //if (!string.IsNullOrEmpty(Request.Form[this.ddlCity.Name]) && string.IsNullOrEmpty(Request.Form[this.ddlCountry.Name]))
                //{
                //    areaId = Request.Form[this.ddlCity.Name];
                //    areaId = areaId.Substring(0, 4);
                //}

                //if (!string.IsNullOrEmpty(Request.Form[this.ddlCity.Name]) && !string.IsNullOrEmpty(Request.Form[this.ddlCountry.Name]))
                //{
                //    areaId = Request.Form[this.ddlCountry.Name];
                //}


                if (areaId != Constants.CURRENT_DEPLOY_AREAID)
                {
                    if (areaId.Substring(areaId.Length - 2, 2).Equals("00"))
                    {
                        //2011.3.21 张雄 modify
                        //市级取前4位
                        areaId = areaId.Substring(0, 4);
                    }
                }
                return areaId;
            }
        }



        /// <summary>
        /// js组件默认的地区串。
        /// </summary>
        private string DefaultAreaIDs
        {
            get
            {
                string areaIds = "";

                string defaultProvince = "";
                string defaultCity = "";
                string defaultCountry = "";

                DefaultAreaID = string.IsNullOrEmpty(DefaultAreaID) ? InitAreaID : DefaultAreaID;

                //如果是市级中心机构，则默认选中具体城市
                if (RegexValid(DefaultAreaID, "^[0-9]{6}$"))
                {
                    if (DefaultAreaID.Substring(2, 2) != "00")
                    {
                        defaultProvince = DefaultAreaID.Substring(0, 2) + "0000";
                        defaultCity = DefaultAreaID.Substring(0, 4) + "00";

                        if (DefaultAreaID.Substring(4, 2) != "00")
                        {
                            defaultCountry = DefaultAreaID;
                        }
                    }
                    else
                    {
                        defaultProvince = DefaultAreaID.Substring(0, 2) + "0000";
                    }
                }

                //省份

                if (!string.IsNullOrEmpty(Request.Form[this.ddlProvicedl.Name]))
                {
                    areaIds += "," + "\"" + Request.Form[this.ddlProvicedl.Name] + "\"";
                }
                else if (!string.IsNullOrEmpty(defaultProvince))
                {
                    areaIds += "," + "\"" + defaultProvince + "\"";
                }
                //城市

                if (!string.IsNullOrEmpty(Request.Form[this.ddlCitydl.Name]))
                {
                    areaIds += "," + "\"" + Request.Form[this.ddlCitydl.Name] + "\"";
                }
                else if (!string.IsNullOrEmpty(defaultCity))
                {
                    areaIds += "," + "\"" + defaultCity + "\"";
                }
                //区县
                if (!string.IsNullOrEmpty(Request.Form[this.ddlCountrydl.Name]))
                {
                    areaIds += "," + "\"" + Request.Form[this.ddlCountrydl.Name] + "\"";
                }
                else if (!string.IsNullOrEmpty(defaultCountry))
                {
                    areaIds += "," + "\"" + defaultCountry + "\"";
                }

                return areaIds;
            }
        }

        /// <summary>
        /// 验证字符串是否匹配指定的正则表达式
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private bool RegexValid(string input, string pattern)
        {
            Regex regex = new Regex(pattern);
            if (input != null)
            {
                return regex.IsMatch(input);
            }
            else
            {
                return false;
            }
        }
    }
}